package com.lin.mr.maizilaundry.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lin.mr.maizilaundry.R;
import com.lin.mr.maizilaundry.utils.CountEvent;
import com.lin.mr.maizilaundry.utils.ShareUtils;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * 由于引导页的图片大小不能占据整个手机屏幕，所以这里创建了三个不同的布局来将三张图片作为背景
 */
public class GuideActivity extends Activity {

    private ViewPager viewPager;
    private ImageButton btn_guide;
    private List<View> views;
    private LinearLayout dot;
    private ImageView[] imageDot;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guid);
        viewPager = (ViewPager) findViewById(R.id.viewPager_guide);
        btn_guide = (ImageButton) findViewById(R.id.btn_guide);

        initViewPager();
        viewPager.setAdapter(new MyAdapter());
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            //页面滚动时调用
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            //页面被选中时调用
            @Override
            public void onPageSelected(int position) {
                for(int i=0;i < imageDot.length;i++){
                    if(i == position){
                        imageDot[i].setBackgroundResource(R.mipmap.dot_selected);
                    }
                    else{
                        imageDot[i].setBackgroundResource(R.mipmap.dot_unselected);
                    }
                }
            }

            @Override
            //页面状态改变时调用
            public void onPageScrollStateChanged(int state) {

            }
        });

        ShareUtils.putFirstTime(this,true);
    }

    class MyAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(views.get(position));
            return views.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(views.get(position));
        }
    }

    private void initViewPager() {
        views = new ArrayList<>();

        dot = (LinearLayout) findViewById(R.id.dot);
        //这里的写法是将三个布局填充进去
        LayoutInflater mLi = LayoutInflater.from(this);
        View guide1 = mLi.inflate(R.layout.guide1, null);
        View guide2 = mLi.inflate(R.layout.guide2, null);
        View guide3 = mLi.inflate(R.layout.guide3, null);

        views.add(guide1);
        views.add(guide2);
        views.add(guide3);

        //初始化导航点的数组
        imageDot = new ImageView[views.size()];

        for (int i = 0; i < views.size(); i++) {
            ImageView imageView = new ImageView(this);
            //创建一个ImageView来放置圆点，并确定ImageView的宽高
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            imageView.setLayoutParams(params);
            imageDot[i] = imageView;
            //由于刚进去引导页时出现的是第一个引导页，故此时的导航点设置为红色
            if (i == 0) {
                imageDot[i].setBackgroundResource(R.mipmap.dot_selected);
            } else {
                imageDot[i].setBackgroundResource(R.mipmap.dot_unselected);
            }
            dot.addView(imageView);
        }

        //get(2)表示从第三个引导界面获取ImageButton的id
        btn_guide = (ImageButton) views.get(2).findViewById(R.id.btn_guide);

        //ImageButton的点击事件,点击第三张页卡上的按钮进入登录页面
        btn_guide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sp = getSharedPreferences("UserInfo",MODE_PRIVATE);
                String phoneNum = sp.getString("phoneNumber","");
                if (phoneNum != ""){
                    startActivity(new Intent(GuideActivity.this,HomeActivity.class));
                    EventBus.getDefault().post(new CountEvent(5));//让FragmentMe更新名字和手机号
                }else{
                    Intent intent2 = new Intent(GuideActivity.this, LoginActivity2.class);
                    startActivity(intent2);
                }
                finish();
            }
        });

    }
}
